#include "geneticoperations.h"

int DeltaValue(int value, int delta){
	if (random()%2 == 0){
		return value + (random()%delta);
	} else{
		return value - (random()%delta);
	}
}

int Clamp(int value, int min, int max){
	if (value < min)
		return min;
	else if (value > max)
		return max;
	else
		return value;
}

void DeltaMutatePoint(Point* p, int xBound, int yBound, int randDelta){
	p->x = DeltaValue(p->x,randDelta);
	p->y = DeltaValue(p->y,randDelta);
	
	p->x = Clamp(p->x, 0, xBound);
	p->y = Clamp(p->y, 0, yBound);
}

void DeltaMutateColor(Color* cl, int delta){
	cl->r = DeltaValue(cl->r,delta);
	cl->g = DeltaValue(cl->g,delta);
	cl->b = DeltaValue(cl->b,delta);
	cl->a = DeltaValue(cl->a,delta);
	
	cl->r = Clamp(cl->r, 0, 255);
	cl->g = Clamp(cl->g, 0, 255);
	cl->b = Clamp(cl->b, 0, 255);
	cl->a = Clamp(cl->a, 0, 1);
}

void DeltaMutateTriangle(Triangle* t, int shapeDelta, int colorDelta, int xBound, int yBound){
	if (random() % 2 == 0){
		DeltaMutateColor(&(t->cl), colorDelta);
	} else {
		int i;
		for (i = 0; i < random()%3; i++){
			DeltaMutatePoint(&(t->points[i]), xBound, yBound, shapeDelta);
		}
	}
}
